<%@page import="com.xe.gae.control.XEResourcesManager"%>
<%@page import="com.google.appengine.api.datastore.Transaction"%>
<%@page import="com.google.appengine.api.datastore.FetchOptions"%>
<%@page import="com.google.appengine.api.datastore.KeyFactory"%>
<%@page import="com.xe.gae.control.XEDataStorageService"%>
<%@page import="com.google.appengine.api.datastore.Entity"%>
<%@page import="com.google.appengine.api.datastore.DatastoreService"%>
<%@page import="com.google.appengine.api.datastore.DatastoreServiceFactory"%>
<%@page import="com.google.appengine.api.datastore.PreparedQuery"%>
<%@page import="com.xe.gae.model.XEResource"%>
<%@page import="com.google.appengine.api.datastore.Query"%>
<%@page import="java.util.Iterator"%>
<%@page import="com.google.appengine.api.blobstore.BlobInfoFactory"%>
<%@page import="com.google.appengine.api.blobstore.BlobInfo"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>


<style>
html, body {
 height: 100%;
	width: 100%;
}
</style>

 <body>
 
	<table >
	<tr>
		<td><input type="text" name="filter" value="<%=request.getParameter("filter")%>"></td>
		<td><input type="button" value="Search"></td>
		<td><input type="button" value="Upload" onclick="window.location.href='./upload.jsp'"></td>
		<td><input type="button" value="Folder+" onclick="window.location.href='./newfolder.jsp'"></td>
	</tr>
	</table>

	<table >
	<tr>
		<th>FileName</td>
		<th>Size</td>
		<th>Date</td>
		<th>action</td>
	</tr>
	
	
	<% 
	

	
		
	Query q = new Query(XEResource.class.getSimpleName());
	XEResourcesManager rm= new XEResourcesManager();
	XEResource root=rm.getRoot();
	q.setAncestor(root.getTree().getKey());
	// Use PreparedQuery interface to retrieve results
	XEDataStorageService dss=new XEDataStorageService();
	DatastoreService datastore = dss.getDatastore();
	

	PreparedQuery pq = datastore.prepare(q);
	BlobInfoFactory bif= new BlobInfoFactory();
	System.out.println("total de filas" + pq.countEntities());

	
	for (Entity result : pq.asIterable()) {
		
		XEResource resource = new XEResource();
		dss.loadByKey(resource, result);
		
		BlobInfo bi=null;
		if(resource.getBlobKey()!=null) bi= bif.loadBlobInfo(resource.getBlobKey());

		if( bi!=null){		//files
	%>
			<tr>
				<td><a target="<%=resource.getPath()%>" href=<%=resource.getPath()%>><%=resource.getPath()%></a></td>
				<td><%=bi.getSize()%></td>
				<td><%=bi.getCreation()%></td>
				<td>
					<a href="/_xe/admin?action=run&resource-key=<%=KeyFactory.keyToString(resource.getKey())%>">run</a>
				<%
				if (rm.getEditor( resource)!=null) {
				%>
					<a target="Edit_<%=resource.getPath() %>" href="/_xe/admin/edit.jsp?resource-key=<%=KeyFactory.keyToString(resource.getKey())%> ">Edit</a>
				<%} %>		
					<a href="/_xe/admin?action=serve&blob-key=<%=bi.getBlobKey().getKeyString()%>&resource-key=<%=KeyFactory.keyToString(resource.getKey())%>">download</a>
					<a href="/_xe/admin/delete.jsp?blob-key=<%=bi.getBlobKey().getKeyString()%>&resource-key=<%=KeyFactory.keyToString(resource.getKey())%>">del</a>
				</td>
			</tr>
	<% 			}
		else{ 		//folders
			%>
			<tr>
				<td><%=resource.getPath()%></td>
				<td></td>
				<td></td>
				<td>
					<a href="/_xe/admin/upload.jsp?resource-key=<%=KeyFactory.keyToString(resource.getKey())%>">upload</a>
					<a href="/_xe/admin/newfolder.jsp?resource-key=<%=KeyFactory.keyToString(resource.getKey())%>">+folder</a>
				<%	if(!resource.getPath().equalsIgnoreCase(root.getPath())){ %> 
					<a href="/_xe/admin/delete.jsp?resource-key=<%=KeyFactory.keyToString(resource.getKey())%>">del</a>
					
				 <% }%>	
				</td>
			</tr>
     <%			
		}
		
	} %>
	
	</table>

 </body>